package com.RecipeBook.search;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

import com.RecipeBook.browse.Browse;
import com.RecipeBook.browse.DataBaseHelper;
import com.RecipeBook.common.RecipeActivity;
import com.RecipeBook.main.GlobalsManager;
import com.RecipeBook.main.R;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.SQLException;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import android.widget.AdapterView.OnItemClickListener;

public class Search extends	RecipeActivity {

	HashMap<String, String> queryData = new HashMap<String,String>();
	AlertDialog alert = null;
	TextView found;
	DataBaseHelper myDbHelper;

	public void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);


		/*myDbHelper = new DataBaseHelper(this);
		try {

			myDbHelper.openDataBase();

		}catch(SQLException sqle){

			throw sqle;

		}*/
		
		myDbHelper = GlobalsManager.getGlobals().getDatabaseHelper();

		setContentView(R.layout.search);
		found = (TextView) findViewById(R.id.found);
		found.setText(getRecipes(queryData).size() + " recipes found!");
		String[] items = new String[]{"Main Ingredient", "Cuisine", "Course", "Dish Type", "Dietary Considerations"}; 
		ArrayAdapter<String> ad=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_single_choice,items);
		final ListView list=(ListView)findViewById(R.id.SearchList);
		list.setAdapter(ad);


		found.setOnClickListener(new OnClickListener()
		{
			public void onClick(View arg0) 
			{
				viewSearchResults(getRecipes(queryData));
			}
		});

		final AlertDialog.Builder builder = new AlertDialog.Builder(this);


		list.setOnItemClickListener(new OnItemClickListener()
		{

			public void onItemClick(AdapterView<?> arg0, final View arg1, int arg2,
					long arg3) 
			{
				final String category = list.getItemAtPosition(arg2).toString();
				final String[] items = getOptions(category);
				builder.setTitle("Choose a " + category);
				builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int item) 
					{
						if(items[item] != "Clear")
						{
							queryData.put(category, items[item]);

							TextView temp=(TextView)arg1;				   
							temp.setText(category + ": " + items[item]);
						}
						else
						{
							for(String temp:queryData.keySet())
							{
								if(temp == category)
								{
									if(queryData.remove(temp) == null)
									{
										System.out.println("ERROR!");
									}
								}
							}
							queryData.remove(category);

							for(String temp:queryData.keySet())
							{
								if(temp == category)
								{
									queryData.remove(temp);
								}
							}
							
							TextView temp=(TextView)arg1;				   
							temp.setText(category);
						}
						alert.dismiss();
						int x = (getRecipes(queryData)).size();
						found.setText(x + " recipes found!");
					}
				});
				alert = builder.create();
				alert.show();
			}

		}
		);
	}

	protected String[] getOptions(String category) 
	{
		/*
		 * TODO: Switch these to db queries
		 */
		if(category == "Main Ingredient")
		{
			return new String[]{"Chicken", "Pork", "Beef", "Vegetables", "Fish", "Clear"};
		}
		else if(category == "Cuisine")
		{
			return new String[]{"American", "Italian", "French", "Mexican", "Asian", "Contemporary", "Clear"};
		}
		else if(category == "Course")
		{
			return new String[]{"Dinner", "Lunch", "Breakfast", "Appetizer", "Dessert", "Clear"};
		}
		else if(category == "Dish Type")
		{
			return new String[]{"Pasta", "Soup", "Sandwich", "Clear"};
		}
		else
		{
			return new String[]{"Vegetarian", "Vegan", "Clear"};
		}
	}

	ArrayList<Integer> getRecipes(HashMap<String, String> queryData)
	{
		ArrayList<Integer> allRecipes = myDbHelper.search(queryData);

		/*ArrayList<Integer> recipeIDs = new ArrayList<Integer>();
		for(int x = 0; x < ((50-(queryData.size())*3)); x++)
		{
			recipeIDs.add(x);
		}*/
		return allRecipes;
	}
}